package com.et.mserver;

import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;

public class MContext {

	// we will store the context into IoSession (controlled by mina)
	public static final AttributeKey SESSION_STATUS_KEY = new AttributeKey(MContext.class, "Abstract MailSession");

	public MContext() {
		this.state = MServerConstants.INIT_STATE;
		this.receivingBinaryLength = 0;
	}

	public void moveToBinaryReceivingState() {

		if (this.state != MServerConstants.INIT_STATE) {
			throw new RuntimeException("failed to move to ready status");
		}

		this.state = MServerConstants.BINARY_DATA_RECEIVING;
	}

	public boolean isInitState() {
		return (this.state == MServerConstants.INIT_STATE);
	}

	public boolean isBinaryReceivingState() {
		return (this.state == MServerConstants.BINARY_DATA_RECEIVING);
	}

	public void append(byte[] bytes, IoSession session) {

		if (bytes == null) {
			throw new RuntimeException(Utility.logHeadOfSession(session) + "append a null bytes");
		}

		this.receivingBinaryLength += bytes.length;
	}

	public long getReceivingBinaryLength() {

		return receivingBinaryLength;
	}

	private int state;

	private long receivingBinaryLength;
}
